<?php
include("../models/entities/class.AccountEntity.php");
@session_start();
if(isset($_SESSION["Username"]))
{
    include("../models/webconfig.php");
    include("../models/class.connection.php");
    include("../models/entities/class.CategoryEntity.php");
    include("../models/repositories/class.CategoryRepository.php");
    include("../models/entities/class.SectionEntity.php");
    include("../models/repositories/class.SectionRepository.php");
    include("../library/library.pager.php");

    include("../models/repositories/class.GroupAuthorityRepository.php");
    if(isset($_SESSION['Account'])){
        $groupAuthorityRepository = new GroupAuthorityRepository();
        $account = $_SESSION['Account'];
        if(!$groupAuthorityRepository->CheckExistGroupAuthority($account->GroupID_i, 5))
            header("location:NoAccess.php");
    }
    else
    {
        header("location:NoAccess.php");
    }

    $categoryRepository = new CategoryRepository();
    $sectionRepository = new SectionRepository();
    $category = null;
    if(isset($_GET['categoryID'])){
        if(is_numeric($_GET['categoryID']))
        {
            $category = $categoryRepository->GetCategory($_GET['categoryID']);
            if($category == null){
                exit("Không tìm thấy category cần chỉnh sửa");
            }
        }
        else{
            exit("Không tìm thấy category cần chỉnh sửa");
        }
    }
    else{
        exit("Không tìm thấy category cần chỉnh sửa");
    }
    $lstSection = $sectionRepository->GetAll();
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Edit Category</title>
<link href="../content/Layout.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="../content/ui-lightness/jquery-ui-1.8.6.custom.css" rel="stylesheet" type="text/css" media="screen"/>
<script type="text/javascript" src="../scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../scripts/jquery.validate.js"></script>
<script type="text/javascript" src="../scripts/jquery-ui-1.8.6.custom.min.js"></script>
</script>
<script type="text/javascript">
    $(document).ready(function () {
        var str = location.href.toLowerCase();

        $('ul#mainNav li a').each(function () {
            if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
             $(this).addClass("active");
        });

        $('ul#sideNav li a').each(function () {
            if (str.indexOf($(this).attr("href").toLowerCase()) > -1)
            $(this).addClass("active");
        });
        
        $("#frmCategory").validate({
			rules: {
				Name: "required"
			},

			messages: {
				Name: "Vui lòng nhập tên category"
			}
		});
    });   
</script>
<script type="text/javascript" src="../scripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
	tinyMCE.init({
		mode: "textareas",
		theme: "advanced",
		plugins: "style,iespell,searchreplace,contextmenu,paste,html",
		forced_root_block : false,
		force_br_newlines : true,
		force_p_newlines : false,    
		convert_newlines_to_brs : true,
		width: "250",

		//&nbsp; Theme options #1
		theme_advanced_buttons1: "bold,italic,underline,formatselect,cut,copy,paste,pastetext,pasteword",
		theme_advanced_buttons2: "fontselect,fontsizeselect,|,link,unlink,code",
		theme_advanced_buttons3: "justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist",
		theme_advanced_buttons4: "",

		//Theme options #2
		theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontselect,fontsizeselect,|,bullist,numlist,|,cut,copy,paste,pastetext,pasteword,|,forecolor,backcolor",
		theme_advanced_buttons2: "",

		// Align and place toolbar
		theme_advanced_toolbar_location: "top",
		theme_advanced_toolbar_align: "left",
		theme_advanced_resizing: true,
		readonly: false,

		//style
		content_css : "../scripts/tiny_mce/css/CustomCSS.css",
		theme_advanced_font_sizes: "12px,13px,14px,16px,18px,20px",
		font_size_style_values : "12px,13px,14px,16px,18px,20px",
	});
</script>
<style type="text/css">
label.error{
    color:red;
    float:left;
}
</style>
</head>

<body>
    <div id="format">
        <div id="wrapper">
        	<!--Ph?n main menu-->
			<?php
				include("../includes/backend_mainnav.php");
			?>
		
            <div id="containerHolder">
                <div id="container" style="color: #646464;">
                	<!--Ph?n main content vi?t t?i dây-->
                    <h3>Sửa category</h3>
                    <div class="clear"></div>
                        <?php
                            if(isset($_POST['ID']))
                            {
                                $id = $_POST['ID'];
                                $name = $_POST['Name'];
                                $description = $_POST['Description'];
                                $sectionID = $_POST['SectionID'];
                                $entity = new CategoryEntity($id, $name, $description, $sectionID);
                                if($categoryRepository->Update($entity) > 0)
                                {
                                    echo "<br/> Sửa category thành công";
                                    $category = $categoryRepository->GetCategory($id);
                                }
                                else
                                {
                                    echo "<br/> Có lỗi xảy ra khi sửa category ! Hãy thử lại";
                                }
                            }                             
                        ?>
                    <fieldset style="background: none; margin:20px auto 0 auto; width: 100%;">
                        <form method="post" id="frmCategory">
                            <div style="float: left; width:150px; text-align: left; margin-top:7px;">ID</div>
                            <div style="float: left; text-align: left; margin-top:7px; width:250px;"><input type="text" readonly="true" id="ID" name="ID" style="background-color: #dedede; width: 200px;" value="<?php echo $category->ID_i; ?>" /></div>
                            <div class="clear"></div>
                            <div style="float: left; width:150px; text-align: left; margin-top:7px;">Tên category</div>
                            <div style="float: left; text-align: left; margin-top:7px; width:250px;"><input class="required" type="text" id="Name" name="Name" style="width: 200px;" value="<?php echo $category->Name_s; ?>" /></div>
                            <div class="clear"></div>
                            <div style="float: left; width:150px; text-align: left; margin-top:7px;">Section</div>
                            <div style="float: left; text-align: left; margin-top:7px; width:250px;">
                                <select name="SectionID">
                                    <?php 
                                    for($i = 0; $i < count($lstSection); $i++){
                                        if($lstSection[$i]->ID_i == $category->SectionID_i){    
                                    ?>
                                    <option value="<?php echo $lstSection[$i]->ID_i ?>" selected="true"><?php echo $lstSection[$i]->Name_s ?></option>
                                    <?php } else {?>
                                    <option value="<?php echo $lstSection[$i]->ID_i ?>"><?php echo $lstSection[$i]->Name_s ?></option>
                                    <?php } } ?>
                                </select>
                            </div>
                            <div class="clear"></div>
                            <div style="float: left; width:150px; text-align: left; margin-top:7px;">Mô tả</div>
                            <div style="float: left; text-align: left; margin-top:7px;">
                                <textarea style="width: 200px; height:150px;" name="Description"><?php echo $category->Description_s; ?></textarea>
                            </div>
                            <div class="clear"></div>
                            <div style="float: left; width:150px; text-align: left; margin-top:7px;"></div>
                            <div style="float: left; text-align: left; margin-top:7px;">
                                <input type="submit" value="Sửa" style="width:70px;"/>
                            </div>
                        </form>
                        
                    </fieldset> 
                 </div>
            <!-- // #container -->
            
            <!--Ph?n footer c?a trang-->
            <?php
            include("../includes/backend_footer.php");
            ?>
            </div>
        </div>
</body>

</html>
<?php
}
else
{
    header("location:../views/Admin_login.php?logout=TRUE");
}
?>